/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.fca.daos;

import db.connectionPool.DBPoolException;
import db.dao.AbstractDAO;
import db.orm.BeansGenerator;
import edu.fca.beans.CalendarioBean;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Romms
 */
public class CalendarioDAO extends AbstractDAO<CalendarioBean>{

    @Override
    public void add(CalendarioBean entity) throws Exception {
        super.setQuery("INSERT INTO calendario (" +
            "codigo," +
            "profesorCodigo," +
            "fecha,"+
            "hora) " +
            "VALUES" + "('"+ entity.getCodigo()+ "','" +
            entity.getProfesorCodigo()+ "','" +
            entity.getFecha()+ "','" +
            entity.getHora()+ "')");
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void deleteWhere(String condition) throws Exception {
        super.setQuery("DELETE FROM calendario WHERE " + condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void updateWhere(CalendarioBean entity,String condition) throws Exception {
        super.setQuery("UPDATE calendario SET " +
            "codigo='"+entity.getCodigo()+
             "',profesorCodigo='"+ entity.getProfesorCodigo()+
             "',fecha='"+ entity.getFecha()+
             "',hora='"+ entity.getHora()+
             "' WHERE "+ condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    protected ArrayList<CalendarioBean> select() throws Exception {
        ArrayList<CalendarioBean> list  =BeansGenerator.getInstance().generateListOfBeans
                (super.connectionManager.getConnection().executeQuery(this.getQuery()), CalendarioBean.class);
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
        return list;
    }

    @Override
    public ArrayList<CalendarioBean> selectAll() throws Exception {
        this.setQuery("SELECT * FROM calendario");
        return this.select();
    }

    @Override
    public ArrayList<CalendarioBean> selectWhere(String condition) throws Exception {
        this.setQuery("SELECT * FROM calendario WHERE "+condition);
        return this.select();
    }

    public int getCountOfAdminCalen(String profesorCodigo){
        try {

            /**
            this.setQuery("SELECT COUNT(*) as count FROM calendario WHERE " +
                    "profesorCodigo='"+profesorCodigo+"' AND fecha>curdate()");
             */
            this.setQuery("SELECT * FROM calendario WHERE " +
                    "profesorCodigo='"+profesorCodigo+"' AND fecha>curdate()");
            System.out.println(this.getQuery());
            int count = this.select().size();
            this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
            return count;
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
        
    }

}
